import Vue from 'vue'
import VueRouter from 'vue-router'
import App from '../App.vue'
import Work from '../views/work/Work.vue'
import Diagnosis from '../views/diagnosis/Diagnosis.vue'
import Manage from '../views/manage/Manage.vue'
import Manage2 from '../views/magnage2/Manage2.vue'
import Retail from '../views/retail/Retail.vue'
import Situation from '../views/situation/Situation.vue'
import Statistics from '../views/statistics/Statistics.vue'
import Vip from '../views/vip/Vip.vue'
import Layout from '../layout/Layout.vue'
import Login from '../views/login/Login.vue'
import Register from '../views/register/Register.vue'
import NotFind from '../views/NotFind.vue'
import Member from '../views/member/Member.vue'
import Updatepassword from '../views/updatepassword/UpdatePassword.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/layout/work' //路由要存在的
    },
    {
        path: '/layout',
        name: 'layout',
        component: Layout,
        children: [
            {
                path: 'work',//工作台
                name: 'work',
                meta: { title: '工作台' },
                component: Work
            },
            {
                path: 'diagnosis',//新开就诊
                name: 'diagnosis',
                meta: { title: '新开就诊' },
                component: Diagnosis
            },
            {
                path: 'manage',//挂号管理
                name: 'manage',
                meta: { title: '挂号管理' },
                component: Manage
            },
            {
                path: 'manage2',//挂号管理2
                name: 'manage2',
                meta: { title: '挂号管理2' },
                component: Manage2
            },
            {
                path: 'retail',//药品零售
                name: 'retail',
                meta: { title: '药品零售' },
                component: Retail
            },
            {
                path: 'situation',//经营状况
                name: 'situation',
                meta: { title: '经营状况' },
                component: Situation
            },
            {
                path: 'statistics',//收费统计
                name: 'statistics',
                meta: { title: '收费统计' },
                component: Statistics
            },
            {
                path: 'vip',//会员管理
                name: 'vip',
                meta: { title: '会员管理' },
                component: Vip
            },


        ]
    }, {
        path: '/register',
        name: 'register',
        component: Register
    }, {
        path: '/login',
        name: 'login',
        component: Login
    }, {
        path: '/updatepassword',
        name: 'updatepassword',
        component: Updatepassword
    },
    {
        path: '*',
        component: NotFind
    }
]


const routeAsync = [
    {
        path: 'member',
        name: 'member',
        meta: { rule: ['管理员'], title: '成员介绍' },
        component: Member,
    }
]
function getRouterFn (rule, routeAsync) {//根据角色做权限路由的筛查
    var rout = [];
    routeAsync.forEach(item => {
        if (item.meta.rule.includes(rule)) {
            rout.push(item)

        }
    })
    return rout
}
var wihteArr = ['/login', '/register', '/updatepassword']//在此数组中的路由完全放行
import { getToken } from '../util/cookie'
const router = new VueRouter({
    routes
})
// 该段代码不需要记，理解即可
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push (location) {
    return originalPush.call(this, location).catch((err) => err);
};
import store from '../store/index.js'
import { getUserApi } from '../api/loginApi.js'
router.beforeEach((to, from, next) => {
    var path = to.path//新的路由对象 from旧的路由对象
    var token = getToken('token')
    if (token) {//有
        if (path == '/login') {//避免重复登陆
            next('/')
        } else {
            //发送请求获取用户的信息
            //判断vuex,如果已经有该用户的值,就不用发送请求,咩有在发送
            if (store.state.routes.length == 0) {
                getUserApi({ token: token }).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        var rule = res.data.rule//获取数据里的角色(管理员/用户)[从登录的用户里取]
                        console.log('rule', rule);
                        store.commit('setUser', res.data)//保存用户的基本信息
                        //制作我们的权限菜单
                        var rou = getRouterFn(rule, routeAsync)//【routeAsync 对比数据库的用户】
                        routes[1].children = routes[1].children.concat(rou)//合并
                        console.log('routes', routes);
                        //保存路由到vuex
                        store.commit('setroutes', routes)
                        // 动态渲染路由即可
                        router.addRoutes(routes)//添加路由
                        next({ ...to, replace: true })//解决addRoute的bug
                    }
                })
            } else {
                next()//如果vuex有值直接放行
            }

        }
    } else {//没有
        if (wihteArr.includes(path)) {
            next()//放行
        } else {
            next('/login')
        }
    }
    next()
})
export default router
